package pers.chy.chyappointmentplatform.Controller;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.chy.chyappointmentplatform.Model.*;
import pers.chy.chyappointmentplatform.Model.NoMapping.ReplyMsgDTO;
import pers.chy.chyappointmentplatform.Service.AppointService;
import pers.chy.chyappointmentplatform.Service.CallNumberService;
import pers.chy.chyappointmentplatform.Socket.Server_PICC;
import pers.chy.chyappointmentplatform.Socket.Server_YM;
import pers.chy.chyappointmentplatform.Utils.ChyException;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("AppointController")
public class AppointController {


    @Autowired
    private pers.chy.chyappointmentplatform.Mapper.HisMapper.Yjyy_PdjhMapper Yjyy_PdjhMapper; //注入Mapper接口

    @Autowired
    private AppointService appointService;

    @Autowired
    private CallNumberService callNumberService;

    @RequestMapping("hello")
    public String hello()
    {
        //查
        Yjyy_PdjhDTO yjyy_PdjhDTO = Yjyy_PdjhMapper.selectMaxRecordToday(1);
        System.out.println(yjyy_PdjhDTO.toString());
        return yjyy_PdjhDTO.toString();
    }


    /**
     * 排队取号
     * @return
     */
    @RequestMapping("takeQueueNumber")
    public String takeQueueNumber(@RequestBody Map params, HttpServletRequest request)
    {
        ReplyMsgDTO replyMsgDTO=new ReplyMsgDTO();

        // 日志
        Logger log = LoggerFactory.getLogger(AppointController.class); // 使用指定的类XXX初始化日志对象，方便在日志输出的时候，可以打印出日志信息所属的类。
        log.info("[Controller入参]："+params);

        // 取号
        boolean isServiceSuccess = false;
        try {
            String ygdm=(String)request.getAttribute("ygdm"); // 从JWT获取当前操作人员
            String ygxm=(String)request.getAttribute("ygxm");
            String ygksmc=(String)request.getAttribute("ksmc");
            String ygzc=(String)request.getAttribute("ygzc");
            Yjyy_PdjhDTO ret = appointService.doTakeQuequeNumber(params,ygdm,ygxm); /* 进入Service层处理业务，根据是否发生异常判断业务是否成功 */
            // 业务成功
            isServiceSuccess= true;
            replyMsgDTO.setStatus(true);
            replyMsgDTO.setDetail(new Gson().toJson(ret));

        } catch (Exception ex) {
            // 业务失败
            isServiceSuccess= false;
            replyMsgDTO.setStatus(false);
            replyMsgDTO.setDetail(ex.toString());
            // 异常日志
            if(ex instanceof ChyException) log.error("[错误]"+ ex.getMessage());
            else log.error("[不期望的异常]",ex);
        }

        if(isServiceSuccess==true){
            /*
             *  向Socket客户端群发叫号数据
             *  Socket失败时不影响Controller的逻辑，仅记录日志
             * */
            try{
                ReplyMsgDTO socketMsgDTO=new ReplyMsgDTO();
                // 获取此队列的最新叫号数据（未叫号、已叫号）
                Map dataMap = callNumberService.getLatestData(2);   //写死：疫苗接种YM
                socketMsgDTO.setStatus(true);
                socketMsgDTO.setDetail(String.format("%s&%s",
                        new Gson().toJson(dataMap.get("wjh")),
                        new Gson().toJson(dataMap.get("yjh"))));
                String socketMsgDTOStr = new Gson().toJson(socketMsgDTO);
                // 群发
                Server_YM.BroadCastInfo(socketMsgDTOStr);   //写死：疫苗接种YM

            }catch (Exception ex){
                // 异常日志
                if(ex instanceof ChyException ) log.error("[错误]"+ ex.getMessage());
                else log.error("[不期望的异常]",ex);
            }
        }


        String replyMsgStr = new Gson().toJson(replyMsgDTO);
        log.info("[Controller出参]："+replyMsgStr);
        return replyMsgStr;
    }

}
